﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TS_XY_Train_Repaire.ToolSystem
{
    // 工具状态 （0:可借 1:已借 2:损坏 3:遗失)
    public enum ToolStatus { Normal, Borrow, Damage, Lose }

    // 工单状态（0:申请 1:待确认 2:待归还 3:已完成）
    public enum OrderStatus { Apply, Confirm, Return, Finish }

    class Utility
    {
        public static string ChangeImageToString(Image image)
        {
            try
            {
                MemoryStream ms = new MemoryStream();
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
                byte[] arr = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(arr, 0, (int)ms.Length);
                ms.Close();
                string pic = Convert.ToBase64String(arr);

                return pic;
            }
            catch (Exception)
            {
                return "";
            }
        }

        public static Image ChangeStringToImage(string pic)
        {
            try
            {
                byte[] imageBytes = Convert.FromBase64String(pic);
                //读入MemoryStream对象  
                MemoryStream memoryStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
                memoryStream.Write(imageBytes, 0, imageBytes.Length);
                //转成图片  
                Image image = Image.FromStream(memoryStream);

                return image;
            }
            catch (Exception)
            {
                Image image = null;
                return image;
            }
        }

        public static Image ChangePhotoSize(Image fullImage, int iWidth, int iHeight)
        {
            var size = new Size(iWidth, iHeight);
            float scaleX = ((float)size.Width) / fullImage.Width;
            float scaleY = ((float)size.Height) / fullImage.Height;
            float scale = scaleX > scaleY ? scaleX : scaleY;

            var resizedImage = new Bitmap(size.Width, size.Height);
            using (Graphics g = Graphics.FromImage(resizedImage))
            {
                size = new Size(
                    (int)(fullImage.Width * scale + 0.5f),
                    (int)(fullImage.Height * scale + 0.5f));
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(fullImage, new Rectangle(Point.Empty, size));
            }

            return resizedImage;
        }

        public static string GetToolStateString(ToolStatus oStatus)
        {
            if (oStatus == ToolStatus.Normal)
            {
                return "可借";
            }
            else if (oStatus == ToolStatus.Borrow)
            {
                return "已借";
            }
            else if (oStatus == ToolStatus.Damage)
            {
                return "损坏";
            }
            else if (oStatus == ToolStatus.Lose)
            {
                return "遗失";
            }

            return "错误类型";
        }

        public static int GetToolStatueInt(string sStatus)
        {
            if (sStatus == "可借")
            {
                return (int)ToolStatus.Normal;
            }
            else if (sStatus == "已借")
            {
                return (int)ToolStatus.Borrow;
            }
            else if (sStatus == "损坏")
            {
                return (int)ToolStatus.Damage;
            }
            else if (sStatus == "遗失")
            {
                return (int)ToolStatus.Lose;
            }

            return -1;
        }
        
        public static string GetOrderStateString(OrderStatus oStatus)
        {
            if (oStatus == OrderStatus.Apply)
            {
                return "已申请";
            }
            else if (oStatus == OrderStatus.Confirm)
            {
                return "待确认";
            }
            else if (oStatus == OrderStatus.Return)
            {
                return "待归还";
            }
            else if (oStatus == OrderStatus.Finish)
            {
                return "已完成";
            }

            return "错误类型";
        }

        public static string GetDateTime(DateTime dateTime)
        {
            if (dateTime == DateTime.MinValue)
                return null;

            return dateTime.ToString();
        }

        public static int GetRowInt(DataRow dataRow, string sColName)
        {
            try
            {
                return (int)dataRow[sColName];
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return -1;
        }

        public static string GetRowString(DataRow dataRow, string sColName)
        {
            try
            {
                return dataRow[sColName].ToString();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return "";
        }

        public static DateTime GetRowDatetime(DataRow dataRow, string sColName)
        {
            try
            {
                return (DateTime)dataRow[sColName];
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return DateTime.MinValue;
        }
    }

    public class StatusFormat : IFormatProvider, ICustomFormatter
    {
        // 0:工具状态 1:工单状态
        private int m_iStatusType = 0;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="FormatText">格式文本:示例(0:待提交|1:待审核|2:已完成)</param>
        public StatusFormat(int iType = 0)
        {
            m_iStatusType = iType;
        }

        public object GetFormat(Type formatType)
        {
            if (formatType == typeof(ICustomFormatter))
            {
                return this;
            }
            else return null;
        }

        private string GetOrderStatus(OrderStatus iType)
        {

            return "";
        }

        private string GetToolStatus(ToolStatus iType)
        {
            if (iType == ToolStatus.Normal)
            {
                return "可借";
            }
            else if (iType == ToolStatus.Borrow)
            {
                return "已借";
            }
            else if (iType == ToolStatus.Damage)
            {
                return "损坏";
            }
            else if (iType == ToolStatus.Lose)
            {
                return "遗失";
            }

            return "";
        }

        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            int iType = int.Parse(arg.ToString());

            if (iType == 0)
                return GetToolStatus((ToolStatus)iType);
            else
                return GetOrderStatus((OrderStatus) iType);
        }
    }

    // 订单结构体
    public class TS_Order
    {
        private int iID = 0;
        private int iProposID = 0;
        private string sProposName = "";
        private DateTime sProposTime = DateTime.MinValue;
        private int iSendID = 0;
        private string sSendName = "";
        private DateTime sSendTime = DateTime.MinValue;
        private int iReciveID = 0;
        private string sReciveName = "";
        private DateTime sReciveTime = DateTime.MinValue;
        private OrderStatus iStatus = 0;

        public int IID { get => iID; set => iID = value; }
        public int IProposID { get => iProposID; set => iProposID = value; }
        public int ISendID { get => iSendID; set => iSendID = value; }
        public int IReciveID { get => iReciveID; set => iReciveID = value; }
        public OrderStatus IStatus { get => iStatus; set => iStatus = value; }
        public string SProposName { get => sProposName; set => sProposName = value; }
        public string SSendName { get => sSendName; set => sSendName = value; }
        public string SReciveName { get => sReciveName; set => sReciveName = value; }
        public DateTime SReciveTime { get => sReciveTime; set => sReciveTime = value; }
        public DateTime SSendTime { get => sSendTime; set => sSendTime = value; }
        public DateTime SProposTime { get => sProposTime; set => sProposTime = value; }
    }
}
